﻿<template>
    <el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false" :visible.sync="visible"
        class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
        <el-row :gutter="15" class="">
            <el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules">
                <el-col :span="24">
                    <el-form-item label="物料组编码" prop="code">
                        <el-input v-model='dataForm.code' placeholder='请输入' clearable required :style='{ "width": "100%" }'
                        maxlength="100" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="物料组名称" prop="name">
                        <el-input v-model='dataForm.name' placeholder='请输入' clearable required :style='{ "width": "100%" }'
                        maxlength="100" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="有效标志" prop="enabledMark">
                        <el-switch v-model='dataForm.enabledMark' required :active-value='1' :inactive-value='0'>
                        </el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if='false'>
                    <el-form-item label="创建人" prop="creatorUserId">
                        <el-input v-model='dataForm.creatorUserId' placeholder='系统自动生成' readonly>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if='false'>
                    <el-form-item label="创建时间" prop="creatorTime">
                        <el-input v-model='dataForm.creatorTime' placeholder='系统自动生成' readonly>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if='false'>
                    <el-form-item label="更新人" prop="lastModifyUserId">
                        <el-input v-model='dataForm.lastModifyUserId' placeholder='系统自动生成' readonly>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if='false'>
                    <el-form-item label="更新时间" prop="lastModifyTime">
                        <el-input v-model='dataForm.lastModifyTime' placeholder='系统自动生成' readonly>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-form>
        </el-row>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取 消</el-button>
            <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import request from '@/utils/request'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
import mgRestAPI from "@/api/q1/basedata/materialGroup"
export default {
    components: {},
    props: [],
    data()
    {
        var val =/^([\u4e00-\u9fa5]|[a-zA-Z0-9])+$/;
        var validatePass = (rule,value,callback)=>{
            if(!value || value.trim().length == 0){
                callback(new Error("请输入物料组编码"));
            }
            else if(value.trim().length > 100){
                callback(new Error("物料组编码长度不能超过100字"));
            }
            else if (!val.test(value)) {
                callback(new Error("物料组编码不能包括特殊字符"));
            }else{
                callback();
         }
        };
        var validatename = (rule,value,callback)=>{
            if(!value || value.trim().length == 0){
                callback(new Error("请输入物料组名称"));
            }else if(value.trim().length > 100){
                callback(new Error("物料组名称长度不能超过100字"));
            }else{
              callback();
              }
        };
        return {
            btnLoading: false,
            loading: false,
            visible: false,
            dataForm: {
                id: '',
                code: undefined,
                name: undefined,
                enabledMark: 1,
                creatorUserId: undefined,
                creatorTime: undefined,
                lastModifyUserId: undefined,
                lastModifyTime: undefined,
            },
            rules: {
                code: [
                    {
                        required: true,
                        //message: '请输入缺陷类别名称',
                        validator: validatePass,
                        trigger: '"blur"'
                    },
                ],
                name: [
                    {
                        required: true,
                        //message: '请输入缺陷类别名称',
                        validator: validatename,
                        trigger: '"blur"'
                    },
                ],
                enabledMark: [
                    {
                        required: true,
                        message: '请输入有效标志',
                        trigger: 'change'
                    },
                ],
            },
        }
    },
    computed: {},
    watch: {},
    created()
    {
    },
    mounted()
    {
    },
    methods: {
        goBack()
        {
            this.$emit('refresh')
        },
        init(id)
        {
            this.dataForm.id = id || 0;
            this.visible = true;
            this.$nextTick(() =>
            {
                this.$refs['elForm'].resetFields();
                if (this.dataForm.id)
                {
                    mgRestAPI.getInfo(this.dataForm.id).then(res =>
                    {
                        this.dataForm = res.data;
                    }).catch((res) =>
                    {
                        this.btnLoading = false;
                    });
                }
            });
            this.$store.commit('generator/UPDATE_RELATION_DATA', {})
        },
        dataFormSubmit()
        {
            this.$refs['elForm'].validate((valid) =>
            {
                if (valid)
                {
                    this.btnLoading = true;
                    if (!this.dataForm.id)
                    {
                        mgRestAPI.create(this.dataForm).then((res) =>
                        {
                            this.$message({
                                message: res.msg,
                                type: 'success',
                                duration: 1000,
                                onClose: () =>
                                {
                                    this.btnLoading = false;
                                    this.visible = false,
                                        this.$emit('refresh', true)
                                }
                            })
                        }).catch((res) =>
                        {
                            this.btnLoading = false;
                        });
                    } else
                    {
                        mgRestAPI.update(this.dataForm.id, this.dataForm).then((res) =>
                        {
                            this.$message({
                                message: res.msg,
                                type: 'success',
                                duration: 1000,
                                onClose: () =>
                                {
                                    this.btnLoading = false;
                                    this.visible = false
                                    this.$emit('refresh', true)
                                }
                            })
                        }).catch((res) =>
                        {
                            this.btnLoading = false;
                        });
                    }
                }
            })
        },
    }
}
</script>
